package com.tz.movie.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tz.movie.domain.Comment;
import com.tz.movie.domain.CommentExample;
import com.tz.movie.domain.Movie;
import com.tz.movie.domain.MovieExample;
import com.tz.movie.domain.User;
import com.tz.movie.domain.UserExample;
import com.tz.movie.domain.status;
import com.tz.movie.mapper.CommentMapper;
import com.tz.movie.mapper.MovieMapper;
import com.tz.movie.mapper.UserMapper;

import org.omg.CORBA.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/comment")
public class CommentController {
  @Autowired
  CommentMapper commentMapper;

  @Autowired
  UserMapper UserMapper;

  @Autowired
  MovieMapper MovieMapper;

  @PostMapping(value = "/getAllComment")
  public String getAllUser(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();

    List<Comment> comments = commentMapper.selectByExample(new CommentExample());
    JSONArray jsonArray = new JSONArray();

    for (Comment comment : comments) {
      JSONObject obj = JSONObject.parseObject(comment.toString());

      UserExample userExample = new UserExample();
      UserExample.Criteria criteria = userExample.createCriteria();
      criteria.andIdEqualTo(comment.getUid());
      List<User> user = UserMapper.selectByExample(userExample);
      if (user.size() > 0)
        obj.put("userdata", JSONObject.parseObject(user.get(0).toString()));

      MovieExample movieExample = new MovieExample();
      MovieExample.Criteria criteria2 = movieExample.createCriteria();
      criteria2.andIdEqualTo(comment.getMid());
      List<Movie> movies = MovieMapper.selectByExample(movieExample);
      if (movies.size() > 0)
        obj.put("moviedata", JSONObject.parseObject(movies.get(0).toString()));
      obj.put("data", JSONObject.parseObject(comment.toString()));
      jsonArray.add(obj);
    }
    result.put("result", status.s_ok);
    result.put("data", jsonArray);
    return result.toJSONString();
  }

  @PostMapping(value = "/getComment")
  public String getComment(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    int movieid = jsonparm.getInteger("id");

    MovieExample movieExample2 = new MovieExample();
    MovieExample.Criteria cc2 = movieExample2.createCriteria();
    cc2.andIdEqualTo(movieid);
    List<Movie> mv = MovieMapper.selectByExample(movieExample2);
    

    CommentExample commentExample =  new CommentExample();
    CommentExample.Criteria cc = commentExample.createCriteria();
    cc.andMidEqualTo(movieid);
    List<Comment> comments = commentMapper.selectByExample(commentExample);
    JSONArray jsonArray = new JSONArray();
 
    for (Comment comment : comments) {
      JSONObject obj = JSONObject.parseObject(comment.toString());

      UserExample userExample = new UserExample();
      UserExample.Criteria criteria = userExample.createCriteria();
      criteria.andIdEqualTo(comment.getUid());
      List<User> user = UserMapper.selectByExample(userExample);
      if (user.size() > 0)
        obj.put("userdata", JSONObject.parseObject(user.get(0).toString()));

      MovieExample movieExample = new MovieExample();
      MovieExample.Criteria criteria2 = movieExample.createCriteria();
      criteria2.andIdEqualTo(comment.getMid());
      List<Movie> movies = MovieMapper.selectByExample(movieExample);
      if (movies.size() > 0)
        obj.put("moviedata", JSONObject.parseObject(movies.get(0).toString()));
       jsonArray.add(obj);
    }
    result.put("basedata", JSONObject.parse( mv.get(0).toString()));
    result.put("result", status.s_ok);
    result.put("data", jsonArray);
    return result.toJSONString();
  }

  @PostMapping(value = "/addComment")
  public String addComment(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    int uid = jsonparm.getInteger("uid");
    int mid = jsonparm.getInteger("mid");
    String content = jsonparm.getString("content");
    Comment comment = new Comment();
    comment.setUid(uid);
    comment.setMid(mid);
    comment.setContent(content);
    commentMapper.insert(comment);
    result.put("result", status.s_ok);
    return result.toJSONString();
  }

  @PostMapping(value = "/updateComment")
  public String updateComment(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();
    return result.toJSONString();
  }

  @PostMapping(value = "/delComment")
  public String delComment(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject result = new JSONObject();

    int delid = jsonparm.getInteger("id");
    CommentExample commentExample = new CommentExample();
    CommentExample.Criteria criteria = commentExample.createCriteria();
    criteria.andIdEqualTo(delid);
    commentMapper.deleteByExample(commentExample);
    result.put("result", status.s_ok);

    return result.toJSONString();
  }
}